<template>
  <div>
    <h1>登录</h1>
    <el-form :model="form" status-icon :rules="rules" label-width="auto">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-radio-group v-model="form.role">
          <el-radio label="user">用户</el-radio>
          <el-radio label="admin">管理员</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const form = ref({
  username: '',
  password: '',
  role: 'user'
});

const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
});

const login = () => {
  // 模拟登录验证
  if (form.value.username === 'admin' && form.value.password === 'admin' && form.value.role === 'admin') {
    sessionStorage.setItem('token', 'admin_token');
    sessionStorage.setItem('role', 'admin');
    router.push({ name: 'AdminDashboard' });
  } else if (form.value.username === 'user' && form.value.password === 'user' && form.value.role === 'user') {
    sessionStorage.setItem('token', 'user_token');
    sessionStorage.setItem('role', 'user');
    router.push({ name: 'UserDashboard' });
  } else {
    alert('用户名、密码或角色错误');
  }
};
</script>

<style scoped>
</style>    